#pragma once

#include "FileReader.h"

class FileReaderMapping : public FileReader
{
public:
	FileReaderMapping();
	virtual ~FileReaderMapping();

	bool Init(IN stringc& strFile, IN u32 useMemory, IN u32 reservedSpace, IN u32 readFlag);
	void Uninit();

	u64 GetFileSize();

	bool GetData(IN u64 offset, OUT u8* data, IN u32 size);
	bool Getu64(IN u64 offset, OUT u64& v);
	bool Getu32(IN u64 offset, OUT u32& v);
	bool Getu16(IN u64 offset, OUT u16& v);
	bool Getu8(IN u64 offset, OUT u8& v);

	void SetReservedSpace(IN u32 reservedSpace);

protected:
	void Clear();
	void CheckParameter();
	bool ReMapping(IN u64 offset, IN u32 size);
	bool ReadFileDirect(IN u64 offset, OUT u8* data, INOUT u32& size);
	bool CheckSize(IN u64 offset1, IN u64 size1, IN u64 offset2, IN u64 size2);		// check if #1 contains #2

private:
	// parameter
	u32 m_useMemory;
	u32 m_reservedSpace;
	u32 m_readFlag;

	//
	HANDLE m_hFile;
	HANDLE m_hMapping;
	u64 m_fileSize;
	u32 m_grain;

	u8* m_view;
	u64 m_fileOffset;
	u32 m_fillSize;

};